package com.ctos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctos.common.Result;
import com.ctos.dto.DishDto;
import com.ctos.entity.Category;
import com.ctos.entity.Dish;
import com.ctos.entity.DishFlavor;
import com.ctos.mapper.DishMapper;
import com.ctos.service.CategoryService;
import com.ctos.service.DishFlavorService;
import com.ctos.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品，同时保存到口味表中
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);


    }

    /**
     * 分页查询 显示菜品分类
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public Result<Page<DishDto>> foodPage(Integer page, Integer pageSize, String name){
        //基础数据page 内不包括菜品信息
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //判断是否传入了名称
        qw.like(name!=null,Dish::getName,name);
        //查询排序根据修改时间
        qw.orderByDesc(Dish::getUpdateTime);
        this.page(pageInfo,qw);


        //创建dto数据对象 将原有对象属性拷贝到新对象中 但是不拷贝records集合
        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");

        //还差存有所有dish对象的records集合 没有拷贝
        //这里获取records集合
        List<Dish> records = pageInfo.getRecords();

        //通过stream流遍历 拷贝所有的dish对象到dishdto对象 现在dishdto对象里面没有CategoryName这个属性
        List<DishDto> dishDtos = records.stream().map((dish) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            return dishDto;
        }).collect(Collectors.toList());

        //遍历获得records内所有的dish id属性
        List<Long> categoryIds = records.stream().map((dish) -> {
            Long categoryId = dish.getCategoryId();
            return categoryId;
        }).collect(Collectors.toList());

        //查询数据库获取所有的菜品分类信息
        List<Category> categorys = categoryService.list();


        //遍历三个集合 所有的菜品信息集合、所有的菜品id集合、所有的基础信息dishdto集合
         for (Long categoryId : categoryIds) {
                for (Category category : categorys) {
                    for (DishDto dishDto : dishDtos) {
                    if (category.getId().equals(categoryId) && dishDto.getCategoryId().equals(categoryId)){
                        //查找三个集合id都匹配上的菜品 即可赋值完成分类
                        dishDto.setCategoryName(category.getName());
                    }
                }
            }
        }
         //将集合存入page对象中
        dishDtoPage.setRecords(dishDtos);

        return Result.success(dishDtoPage);
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override

    public DishDto getDishDto(Long id) {
        DishDto dishDto = new DishDto();

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    /**
     * 修改菜品和口味
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateDishAndDishFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        for (DishFlavor flavor : flavors) {
            flavor.setId(null);
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);



    }

    /**
     * 批量删除和删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(Long[] ids){
        List<Long> list = Arrays.asList(ids);
        this.removeByIds(list);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Long aLong : list) {
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, aLong);
            dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        }


    }

    /**
     * 修改状态
     * @param status
     * @param ids
     */
    @Override
    public void modifyStatus(Integer status, Long[] ids) {
            for (Long id : ids) {
                LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper();
                qw.eq(Dish::getId, id);
                qw.set(id!=null, Dish::getStatus, status);
                this.update(new Dish(),qw);
            }

    }

    /**
     * 查询套餐
     * @param dish
     * @return
     */
    @Override
    public List<DishDto> dishList(Dish dish) {



        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //添加查询条件 起售状态为1
        qw.eq(Dish::getCategoryId, dish.getCategoryId()).eq(Dish::getStatus, 1);
        List<Dish> list = this.list(qw);

        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish temp : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(temp, dishDto);
            dishDtos.add(dishDto);
        }

        for (DishDto dishDto : dishDtos) {
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
        }



        return dishDtos;
    }


}

